package io.zt.dangyuan;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;

import com.igexin.sdk.PushManager;
import com.umeng.analytics.MobclickAgent;


import org.xutils.x;

import io.zt.dangyuan.object.UserModel;

/**
 * Created by 2015-248-pc on 15/7/11.
 */
public class GlobalDataApplication extends Application {

    private UserModel userModel;

    public UserModel getUserModel() {

        userModel = new UserModel();

        SharedPreferences sharedPreferences = getSharedPreferences("data", Context.MODE_PRIVATE);
        String username = sharedPreferences.getString("username", null);
        String userimg = sharedPreferences.getString("userimg", null);
        int userid = sharedPreferences.getInt("userid", 0);
        String realname = sharedPreferences.getString("realname", null);
        String mobile = sharedPreferences.getString("mobile", null);

        if (username == null && userimg == null && realname == null && mobile == null) {
            return null;

        } else {
            if (username != null) {
                userModel.setUsername(username);
            }
            if (userimg != null && !"".equals(userimg)) {
                userModel.setUserimg(userimg);
            }
            if (userid != 0) {
                userModel.setUserid(userid);
            }
            if (realname != null) {
                userModel.setRealname(realname);
            }
            if (mobile != null) {
                userModel.setMobile(mobile);
            }

        }

        return userModel;
    }

    public void setUserModel(UserModel userModel) {

        SharedPreferences sharedPreferences = getSharedPreferences("data", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();//获取编辑器

        if (userModel == null) {
            editor.remove("username");
            editor.remove("userimg");
            editor.remove("realname");
            editor.remove("mobile");
            editor.remove("userid");
            editor.commit();//提交修改

        } else {
            editor.putString("username", userModel.getUsername());
            editor.putString("userimg", userModel.getUserimg());
            editor.putString("realname", userModel.getRealname());
            editor.putString("mobile", userModel.getMobile());
            editor.putInt("userid", userModel.getUserid());
            editor.commit();//提交修改
        }

    }

    @Override
    public void onCreate() {
        super.onCreate();
        x.Ext.init(this);
        x.Ext.setDebug(false);
        PushManager.getInstance().initialize(this);
        MobclickAgent.setScenarioType(this, MobclickAgent.EScenarioType.E_UM_NORMAL);

      //  PlatformConfig.setWeixin("wxffe358a89997c592", "6acd5fb33646432b1095a3565441d718"); //微信 appid appsecret
       //老的登记
//        PlatformConfig.setWeixin("wx5a6f5f30a11eb721", "5341fddbb780c699996093707ae7229d"); //微信 appid appsecret
//        PlatformConfig.setQQZone("1104517944", "c7394704798a158208a74ab60104f0ba");
    }
}
